package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.ChangUserInfoBeanTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserInfoActivityTing extends AppCompatActivity {

    private TextView tvUsernameInfoTing;
    private TextView nichengInfoTing;
    private TextView xingbieInfoTing;
    private TextView xuehaoInfoTing;
    private TextView banjiTing;
    private TextView xingmingInfoTing;
    private TextView kfzInfoTing;
    private TextView qianmingInfoTing;
    private Button xgzlBtnTing;
    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView tvTitleTing;
    private String userNameTing;
    private DataBeanTing<ChangUserInfoBeanTing> r;
    private String nicheng_ting,sex_ting,qianming_ting,xuehao_ting,banji_ting,xingming_ting;
    private int id_ting;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_ting);
        userNameTing = SharedPreferencesUtilsTing.readLoginUserName(this);
        initView();
        initData();
    }

    private void initView() {
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        tvTitleTing = (TextView) findViewById(R.id.tv_title_ting);
        tvUsernameInfoTing = (TextView) findViewById(R.id.tv_usernameInfo_ting);
        nichengInfoTing = (TextView) findViewById(R.id.nichengInfo_ting);
        xingbieInfoTing = (TextView) findViewById(R.id.xingbieInfo_ting);
        xuehaoInfoTing = (TextView) findViewById(R.id.xuehaoInfo_ting);
        banjiTing = (TextView) findViewById(R.id.banji_ting);
        xingmingInfoTing = (TextView) findViewById(R.id.xingmingInfo_ting);
        kfzInfoTing = (TextView) findViewById(R.id.kfzInfo_ting);
        qianmingInfoTing = (TextView) findViewById(R.id.qianmingInfo_ting);
        xgzlBtnTing = (Button) findViewById(R.id.xgzl_btn_ting);
        tvUsernameInfoTing.setText(userNameTing);
        tvTitleTing.setText("个人资料");
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfoActivityTing.this.finish();
            }
        });
        xgzlBtnTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_click();
             }
        });
    }

//    获取个人资料接口的数据
    private void initData() {
        JsonObject jo = new JsonObject();
        jo.addProperty("userName",userNameTing);
//        jo.addProperty("kfz","zxt");
        String url = "userName="+userNameTing;
        OkhttpUtilsTing.isGET("AndroidUser/getUser?"+url, false, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                r = gson.fromJson(res, new TypeToken<DataBeanTing<ChangUserInfoBeanTing>>(){}.getType());
                UserInfoActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(r.code == 200){
                            Log.i("data",res);
                            setData(r.data);
                        }
                    }
                });
            }
        });
    }

    //个人资料的数据渲染到页面
    private void setData(ChangUserInfoBeanTing result) {
//        Log.i("hh",new Gson().toJson(result));
        if(result.nickName == "" || result.nickName == null){
            nichengInfoTing.setText("未填写");
            nichengInfoTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            nichengInfoTing.setText(result.nickName);
            nichengInfoTing.setTextColor(Color.parseColor("#000000"));
        }
        if(result.sex == "" || result.sex == null){
            xingbieInfoTing.setText("未选择");
            xingbieInfoTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            xingbieInfoTing.setTextColor(Color.parseColor("#000000"));
            xingbieInfoTing.setText((result.sex.equals("0"))?"男":(result.sex.equals("1"))?"女":"保密");
        }
        if(result.stuNo == "" || result.stuNo == null){
            xuehaoInfoTing.setText("未填写");
            xuehaoInfoTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            xuehaoInfoTing.setTextColor(Color.parseColor("#000000"));
            xuehaoInfoTing.setText(result.stuNo);
        }
        if(result.stuClass == "" || result.stuClass == null){
            banjiTing.setText("未填写");
            banjiTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            banjiTing.setText(result.stuClass);
            banjiTing.setTextColor(Color.parseColor("#000000"));
        }
        if(result.stuName == "" || result.stuName == null){
            xingmingInfoTing.setText("未填写");
            xingmingInfoTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            xingmingInfoTing.setText(result.stuName);
            xingmingInfoTing.setTextColor(Color.parseColor("#000000"));
        }
        if(result.signature == "" || result.signature == null){
            qianmingInfoTing.setText("\u3000\u3000介绍一下自己吧~");
        }else {
            qianmingInfoTing.setText("\u3000\u3000"+result.signature);
        }
        if(result.kfz == "" || result.kfz == null){
            kfzInfoTing.setText("文化沙漠");
//            kfzInfoTing.setTextColor(Color.parseColor("#c0c0c0"));
        }else {
            kfzInfoTing.setText(result.kfz);
            kfzInfoTing.setTextColor(Color.parseColor("#000000"));
        }

    }

    //跳转到个人修改资料页面
    private void btn_click() {
        //TODO:跳转到个人资料修改页面
        Bundle bdName = new Bundle();
        bdName.putString("NickName",r.data.nickName);
        bdName.putString("Sex",r.data.sex);
        bdName.putString("Signature",r.data.signature);
        bdName.putString("StuNo",r.data.stuNo);
        bdName.putString("StuClass",r.data.stuClass);
        bdName.putString("StuName",r.data.stuName);
        bdName.putInt("id",r.data.id);
        enterActivityForResult(ChangUserInfoActivityTing.class,1,bdName);
    }

    public void enterActivityForResult(Class<?> to,int requestCode, Bundle b){
        Intent i = new Intent(this,to);
        i.putExtras(b);
        startActivityForResult(i,requestCode);
    }

    //从修改页面回传过来的参数
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
//        getString(data);
//        JsonObject jo = new JsonObject();
//        ChangUserInfoBeanTing changUserInfoBeanTing = new ChangUserInfoBeanTing();
//        changUserInfoBeanTing.setNickName(nicheng_ting);
//        changUserInfoBeanTing.setSignature(qianming_ting);
//        changUserInfoBeanTing.setStuNo(xuehao_ting);
//        changUserInfoBeanTing.setStuClass(banji_ting);
//        changUserInfoBeanTing.setStuName(xingming_ting);
//        setData(changUserInfoBeanTing);
    }

    //获取页面传过来的数据
    private void getString(Intent data){
        if(data!=null){
            nicheng_ting = data.getStringExtra("NickName");
            qianming_ting = data.getStringExtra("Signature");
            xuehao_ting = data.getStringExtra("StuNo");
            banji_ting = data.getStringExtra("StuClass");
            xingming_ting = data.getStringExtra("StuName");
        }
    }

}